<?php

class log_class{

private $path='log'; //当前模块目录
private $dbver=100; //当前模块的数据库版本

public function __construct(){
	global $king;

	$dbver=(int)$king->getModuleVer($this->path);
	if($this->dbver > $dbver){
		if($this->install_update($dbver)){//成功则返回True，则更新数据库中的版本信息
			$array=array(
				'ndbver'=>$this->dbver,
			);
			$king->db->update('%s_module',$array,"kpath='{$this->path}'");
		}
	}
}

/* ------>>> 安装部分 <<<---------------------------- */

public function install(){
	global $king;

	$this->install_update(100);

	//写模块安装记录
	if(!$king->db->getRows_one('SELECT * FROM %s_module where kpath=\''.$this->path.'\';')){
		$_array=array(
			'kname' =>$king->lang->get($this->path.'/name'),
			'kpath' =>$this->path,
			'kdb'   =>'',
			'ndate' =>time(),
			'ndbver'=>$this->dbver,
			'norder'=>$king->db->neworder('%s_module'),
			'nshow'=>0,
			);
		$kid1=$king->db->insert("%s_module",$_array);




	$sql='rot char(12) not null,
	ip char(20) not null,
	url char(200) not null,
	http char(200) not null,
	zj char(200) not null,
	lailu char(200) not null,
	qita char(200) not null,
	ndate int(11) not null,
	INDEX(rot)';
	$king->db->createTable('%s_log_fuq',$sql,'kid');

	
	$sql2='ktitle char(200) not null,
	ndate int(11) not null,
	INDEX(ktitle)';
	$king->db->createTable('%s_log_list',$sql2,'kid');
	log_class::install_update();

		$king->db->query("delete from %s_bot where kid >0");
		$_array_bot=array(
			'Baidu'=>'Baiduspider/2.0;',
			'Google'=>'Googlebot/',
			'腾讯搜搜'=>'Sosospider',
			'百度图片'=>'Baiduspider-image+',
			'有道'=>'YoudaoBot',
			'搜狗'=>'Sogou+web+spider',
			'MSN'=>'msnbot',
			'雅虎'=>'Yahoo',
			'360爬虫'=>'360Spider',
			'谷歌图片'=>'Googlebot-Image',
		);
		foreach($_array_bot as $_key => $_value){
			$_array=array(
				'kname'=>$_key,
				'kmark'=>$_value,
			);
			$king->db->insert('%s_bot',$_array);
		}


		return true;
	}else{
		return false;
	}
}




public function king_rot($rot=''){
	global $king;
	//获取IP
	if(empty($rot)) return $rot;
	$karray=$king->db->getRows("select ndate,ktitle from king_log_list where rotname = '".$rot."' order by ktitle asc");
	if(empty($karray)){
		log_class::install_update();
		log_class::gegx_ip();
	}
	return $karray;
}



public function rotsql_count($name='',$rot='',$ip='',$url='',$rq='',$rq2=''){	
	//根据条件获取总数
		global $king;
		if(!empty($name)){
		$karray=$king->db->getRows_one("select count(".$name.") from %s_log_fuq ".log_class::weherk($rot,$ip,$url,$rq,$rq2));
		}
		return  $karray['0'];
}

public function rotsql_var($name='',$rot='',$ip='',$url='',$rq='',$rq2=''){	
	//根据条件获取单条内容
		global $king;
		if(!empty($name)){

			$karray=$king->db->getRows("select * from %s_log_fuq ".log_class::weherk($rot,$ip,$url,$rq,$rq2)." group by ".$name." order by count(".$name.") desc limit 10");
			return  $karray;
		}
}



public function rotsql_list($rot='',$ip='',$url='',$rq='',$rq2=''){	
	//根据条件获取总数
		global $king;
		$karray=$king->db->getRows("select kid,rot,ip,url,http,zj,ndate,DATE_FORMAT(FROM_UNIXTIME(ndate),'%Y-%m-%d') from %s_log_fuq ".log_class::weherk($rot,$ip,$url,$rq,$rq2)."ORDER BY ndate desc");
		return  $karray;
}

public function gegx_ip($ndate=''){
	//更新服务器IP
		global $king;

	if(empty($ndate)){
		$i=$king->db->getRows("select rot,ip from king_log_fuq group by ip");	
	}else{
		$i=$king->db->getRows("select rot,ip from king_log_fuq where ndate > UNIX_TIMESTAMP('".$date."') group by ip");	
	}

		foreach($i as $v){ 


						$ary['rotname']=$v['rot'];
						$ary['ktitle']=$v['ip'];
						$thred=$king->db->getRows_one("select ktitle from %s_log_list where ktitle='".$v['ip']."'");	
		
						if(empty($thred)){
							$king->db->insert('%s_log_list',$ary);
						}
				
		}
}


public function weherk($rot='',$ip='',$url='',$rq='',$rq2=''){	
	//生成WEHER语句

		if(empty($rq2))$rq2 = $rq;

		if(!empty($ip)){
			if(!preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/",$ip)) {
				$s[]="ip like '%".$ip."%'";
			}else{
				$s[]="ip = '".$ip."'";			
			}
		}

		if(!empty($url)) $s[]="url = '".$url."'";
		if(!empty($rq)) $s[]="ndate between  UNIX_TIMESTAMP('".$rq." 00:00:00') and UNIX_TIMESTAMP('".$rq2." 23:59:59')";
		if(!empty($rot)) $s[]="rot = '".$rot."'";
		$d=implode(' and ',$s);

		return  'where '.$d;
}





public function randomColor() {
    $str = '#';
    for($i = 0 ; $i < 6 ; $i++) {
        $randNum = rand(0 , 15);
        switch ($randNum) {
            case 10: $randNum = 'A'; break;
            case 11: $randNum = 'B'; break;
            case 12: $randNum = 'C'; break;
            case 13: $randNum = 'D'; break;
            case 14: $randNum = 'E'; break;
            case 15: $randNum = 'F'; break;
        }
        $str .= $randNum;
    }
    return $str;
}









public function install_update(){
    global $king;
    //增加回复字段
    $king->db->query('ALTER TABLE %s_log_list ADD (
	rotname char(12) null,
	INDEX (rotname),
	INDEX (ktitle),
	nreply tinyint(1) unsigned NOT NULL default 0);');
	log_class::install_fu();
    return true;
}
public function install_fu(){
    global $king;
	$king->db->query('ALTER TABLE %s_log_fuq ADD (
	INDEX (ndate),
	INDEX (ip),
	INDEX (rot));');
	return true;
}

public function get($name,$b){

		$path=$b."/$name.log";
echo $path;
		if (is_file($path)) {
			$text=file_get_contents($path);
			return $text;
		}


	}

	/**
	 * 删除缓存
	 * @param string $name 键值
	 */
public function del($name,$b){

		$path=$b."/$name.log";
		if(is_file($path)){
			unlink($path);
		}

	}





public function ChangeNTE($Date = '09/Oct/2012:07:50:13') {
		$Date=str_replace('[','',$Date);
        $MonthArray = array(
                'Jan' => '01',
                'Feb' => '02',
                'Mar' => '03',
                'Apr' => '04',
                'May' => '05',
                'Jun' => '06',
                'July' => '07',
                'Aug' => '08',
                'Sep' => '09',
                'Oct' => '10',
                'Nov' => '11',
                'Dec' => '12'
                );
        $DateExplode = explode('/', $Date);
		$Datek = explode(':', $DateExplode[2]);
        return  strtotime($DateExplode[0].'-'.$MonthArray[$DateExplode[1]].'-'.$Datek[0].' '.$Datek[1].':'.$Datek[2].':'.$Datek[3]);
}




public function kui_add($luj,$url){
	global $king;


		



		$cachepath="log/chanshu";
		if(!$karray=$king->cache->get($cachepath)){
		
				$i=$king->db->getRows_one("select kcontent from %s_block where kname='日志路径'");	
				$karray['logdata']=$king->db->getRows("select kmark,kname from %s_bot");		
				$karray['url']=explode('|',$i['kcontent']);
				$king->cache->put($cachepath,$karray);

		}		












	$log=log_class::get($url,$luj);

	$log=explode(chr(10),$log);
	$logdata=$karray['logdata'];

////////////////////////////////////////////////////




	

	foreach($log as $value){   
	    $search_data=explode(' ',$value); 

		if($search_name=='baidu.com' or $search_name=='google') {
			$data=$search_data[10];
			$search_zt=$search_data[8];

		}else{

			$data=$value;
			$search_zt=$search_data[8];
		}
			if($sel) 
			{
							
				if(strpos($data,$search_name) and $search_zt==$sel) $show_data.=$value.chr(10);
				
			}
			else
			{			
					foreach($logdata as $values){   
						if(strpos($data,$values['kmark'])){ $show_data.= $values['kname'].' '.$value.chr(10);}
					}
			}

	}

	$show_data=explode(chr(10),$show_data);




//print_r($show_data);
//echo strtotime('09-10-2012 13:38:22');
//echo date('Y-m-j g:i:s',strtotime('09-10-2012 13:38:22'));
//echo gmdate("d-m-Y g:i:s",strtotime('09-Oct-2012:07:50:13'));
//echo date('Y-M-j','09/Oct/2012:07:50:13');


foreach($show_data as $key=> $value){

		$resk[$key]=explode(' ',$value);

		if(!empty($resk[$key][0])){
			$res[$key]['ip']=$resk[$key][1];
			$res[$key]['ndate']=log_class::ChangeNTE($resk[$key][4]);
			$res[$key]['url']=$resk[$key][7];
			$res[$key]['http']=$resk[$key][9];
			$res[$key]['zj']=$resk[$key][10];
			$res[$key]['rot']=$resk[$key][0];
			$res[$key]['lailu']=$resk[$key][11];
			$res[$key]['qita']=$resk[$key][12].$resk[$key][13].$resk[$key][14].$resk[$key][15].$resk[$key][16].$resk[$key][17].$resk[$key][18];
		}

			if(!empty($res[$key])){
			$king->db->insert('%s_log_fuq',$res[$key]);
			}
}





}








}

?>
